package com.zichuan;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author zichuan
 * @version 1.0
 * @Package : com.zichuan
 * @Description : TODO
 * @Create on : 2025/3/19 17:19
 */
public class Test {
    public static void main(String[] args) {
        test2();
    }

    private static void test2() {
        List<String> strings = new ArrayList<>(Arrays.asList("a", "b", "c"));
        for (int i = 0; i < 4; i++) {
            if (i == 3){
                //strings.add("d"); //java.lang.UnsupportedOperationException
                continue;
            }
            //strings.set(i, strings.get(i) + i);
            if (i==2){
                //strings.remove(i);  //java.lang.UnsupportedOperationException
            }
        }
        for (int i = strings.size() - 1; i >= 0; i--) {
            if (i==0){
                strings.remove(i); //java.lang.UnsupportedOperationException
            }
        }
        for (String string : strings) {
            if ("c".equals(string)){
                //strings.remove(string); //java.lang.UnsupportedOperationException
            }
        }
        Iterator<String> iterator = strings.iterator();
//        while (iterator.next() != null){
//            System.out.println(iterator.next());//java.util.NoSuchElementException
//        }
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next);
            if ("c".equals(next)){
                //strings.remove(next); //java.lang.UnsupportedOperationException
                //iterator.remove();   //java.lang.UnsupportedOperationException
            }
        }
        ListIterator<String> stringListIterator = strings.listIterator();
        while (stringListIterator.hasNext()) {
            String next = stringListIterator.next();
            System.out.println(next);
            if ("c".equals(next)){
                stringListIterator.remove();   //java.lang.UnsupportedOperationException
            }
        }

        for (String string : strings) {
            System.out.println(string);
        }
    }

    public static void test1(){
        List<Integer> list = new ArrayList<>();
        list.add(12);
        Class<? extends List> clazz = list.getClass();
        Method add = null;
        try {
            //但是通过反射添加，是可以的
            add = clazz.getDeclaredMethod("add", Object.class);
            add.invoke(list, "kl");
            System.out.println(list);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
